﻿using Pivotbrowser.Models;
using Pivotbrowser.Web;
using Pivotbrowser.Web.Services;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Pivotbrowser.Views
{
    public partial class PrüfAnalysePopup : UserControl
    {

        public class CheckItem
        {
            public bool IsChecked { get; set; }
            public string Text { get; set; }
        }

        public List<CheckItem> PrüfSemList { get; set; }
        public List<CheckItem> PrüfFachSemList { get; set; }
        public List<CheckItem> PrüfFachList { get; set; }
        public List<CheckItem> PrüfNoteList { get; set; }
        public Popup Popup { get; set; }

        public ObservableCollection<MinimalStudent> ItemsSource { get; set; }
        public ObservableCollection<MinimalStudent> Result { get; set; }

        public PrüfAnalysePopup(Popup p, IEnumerable isc)
        {
            InitializeComponent();
            Popup = p;
            ItemsSource = isc as ObservableCollection<MinimalStudent>;

            PrüfSemList = new List<CheckItem>();
            PrüfFachSemList = new List<CheckItem>();
            PrüfFachList = new List<CheckItem>();
            PrüfNoteList = new List<CheckItem>();



            var context = new ContentDomainContext();
            context.Load(context.GetPrüfung_StudentQuery(), (loadOp) =>
            {
                foreach (Prüfung_Student e in loadOp.Entities)
                {
                    if (PrüfSemList.Where(x => x.Text.StartsWith(e.SEMESTERNR)).Count() == 0)
                        PrüfSemList.Add(new CheckItem() { IsChecked = false, Text = e.SEMESTERNR + ": " + e.SEMESTER.BEZ });

                    var str = e.PRÜFUNGSFACH == null ? "K.A." : e.PRÜFUNGSFACH.BEZ;
                    if (e.TEILPRÜFUNG != null && PrüfFachList.Where(x => x.Text.StartsWith(e.TEILPRÜFUNG.ZEUGNISBEZ)).Count() == 0)
                        PrüfFachList.Add(new CheckItem() { IsChecked = false, Text = e.TEILPRÜFUNG.ZEUGNISBEZ == "Seminar" ? "Seminar in " + str : e.TEILPRÜFUNG.ZEUGNISBEZ });

                    if (PrüfNoteList.Where(x => x.Text.StartsWith(e.NOTE_) || x.Text.StartsWith(e.PRÜFUNGSBEMERKUNG.BEZ)).Count() == 0)
                    {
                        var item = new CheckItem() { IsChecked = false, Text = e.NOTE_ == "(null)" ? (e.PRÜFUNGSBEMERKUNG.BEZ == "<Ohne>" ? "Ohne Bemerkung (nicht bestanden)" : e.PRÜFUNGSBEMERKUNG.BEZ) : e.NOTE_ };
                        if (PrüfNoteList.Where(x => x.Text.StartsWith(item.Text)).Count() == 0 )
                            PrüfNoteList.Add(item);
                    }
                    }

                PrüfFachSemList.Add(new CheckItem() { Text = "1" });
                PrüfFachSemList.Add(new CheckItem() { Text = "2" });
                PrüfFachSemList.Add(new CheckItem() { Text = "3" });
                PrüfFachSemList.Add(new CheckItem() { Text = "4" });
                PrüfFachSemList.Add(new CheckItem() { Text = "5" });
                PrüfFachSemList.Add(new CheckItem() { Text = "6" });
                PrüfFachSemList.Add(new CheckItem() { Text = "7" });
                PrüfFachSemList.Add(new CheckItem() { Text = "8" });
                PrüfFachSemList.Add(new CheckItem() { Text = "9" });
                PrüfFachSemList.Add(new CheckItem() { Text = "10" });
                PrüfFachSemList.Add(new CheckItem() { Text = "11" });
                PrüfFachSemList.Add(new CheckItem() { Text = "12" });
                PrüfFachSemList.Add(new CheckItem() { Text = "13" });
                PrüfFachSemList.Add(new CheckItem() { Text = "14" });
                PrüfFachSemList.Add(new CheckItem() { Text = "15" });
                PrüfFachSemList.Add(new CheckItem() { Text = "16" });

                this.PrüfSem.ItemsSource = PrüfSemList.OrderByDescending(x => x.Text);
                this.PrüfFach.ItemsSource = PrüfFachList.OrderBy(x => x.Text);
                this.PrüfNote.ItemsSource = PrüfNoteList.OrderBy(x => x.Text);
                this.PrüfFachSem.ItemsSource = PrüfFachSemList;
            }, null);

        }

        public void Button_Click(object sender, RoutedEventArgs e)
        {
                Result = new ObservableCollection<MinimalStudent>();
                foreach (MinimalStudent ms in ItemsSource)
                {
                    Result.Add(ms);
                }


                List<string> rs_list = new List<string>();
                foreach (CheckItem ci in PrüfSemList)
                {
                    if (ci.IsChecked)
                    {
                        rs_list.Add(ci.Text);
                    }
                }


                List<string> pf_list = new List<string>();
                foreach (CheckItem ci in PrüfFachList)
                {
                    if (ci.IsChecked)
                    {
                        pf_list.Add(ci.Text);
                    }
                }

                List<string> fs_list = new List<string>();
                foreach (CheckItem ci in PrüfFachSemList)
                {
                    if (ci.IsChecked)
                    {
                        fs_list.Add(ci.Text);
                    }
                }

                List<string> no_list = new List<string>();
                foreach (CheckItem ci in PrüfNoteList)
                {
                    if (ci.IsChecked)
                    {
                        no_list.Add(ci.Text);
                    }
                }


                foreach (MinimalStudent ms in Result)
                {
                    try
                    {
                        ms.ResetCollection();

                        ms.FilterCollectionByRelevantSemesters(rs_list);
                        ms.FilterCollectionByFachSemesters(fs_list);
                        ms.FilterCollectionByPruefFach(pf_list);
                        ms.FilterCollectionByNote(no_list);

                        ms.UpdateListRelevantSemester();
                        ms.UpdateListPruefFach();
                        ms.UpdateListFachSem();
                        ms.UpdateListNote();
                    }
                    catch
                    {
                        continue;
                    }
                }

            FilteringDone(this, new EventArgs());
        }
        public event FilteringDoneEventHandler FilteringDone;
        public delegate void FilteringDoneEventHandler(object sender, EventArgs eargs);

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            Popup.IsOpen = false;
        }

    }
}
